package com.example.jz.noone.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jz.noone.utils.BaseActivity;
import com.example.jz.noone.utils.Code;
import com.example.jz.noone.R;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class LoginActivity extends BaseActivity {
    @BindView(R.id.username)
    EditText username;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.yanzheng)
    EditText yanzheng;
    @BindView(R.id.iv_showCode)
    ImageView ivShowCode;
    @BindView(R.id.login)
    Button login;
    @BindView(R.id.register)
    TextView register;
    @BindView(R.id.forget)
    TextView forget;
    private static final String API="http://192.168.30.120:8080/Server";
    private String realCode;
    public static final String TAG = RegisterActivity.class.getName();
    private Toast mToast;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.login_main );
        ButterKnife.bind( this );
        ivShowCode.setImageBitmap( Code.getInstance().createBitmap() );
        realCode=Code.getInstance().getCode().toLowerCase();
    }

    @OnClick({R.id.iv_showCode,R.id.login,R.id.register,R.id.forget})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_showCode:
                ivShowCode.setImageBitmap(Code.getInstance().createBitmap());
                realCode = Code.getInstance().getCode().toLowerCase();
                Log.v(TAG,"realCode"+realCode);
                break;
            case R.id.login:
                String name = username.getText().toString();
                String pwd = password.getText().toString();
                String yz = yanzheng.getText().toString().toLowerCase();
                if (TextUtils.isEmpty( name )) {
                    showToast( "登录账号不能为空!" );
                    return;
                }
                if (TextUtils.isEmpty( pwd )) {
                    showToast( "密码不能为空!" );
                    return;
                }
                if (TextUtils.isEmpty( yz )){
                    showToast( "验证码不能为空!" );
                }
                if (yz.equals(realCode)){
                    getData(name,pwd);
                }else {
                    showToast( "验证码错误!" );
                    ivShowCode.setImageBitmap(Code.getInstance().createBitmap());
                    realCode = Code.getInstance().getCode().toLowerCase();
                    Log.v(TAG,"realCode"+realCode);
                    return;
                }
                break;
            case R.id.register:
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity( intent );
                break;
            case R.id.forget:
                break;
        }
    }
    private void showToast(String msg) {
        if (mToast == null) {
            mToast=Toast.makeText( this,msg,Toast.LENGTH_SHORT );
        } else {
            mToast.setText( msg );
        }
        mToast.show();
    }

    private void getData(final String name,final String pwd) {
        RequestParams params=new RequestParams( API );
        params.addParameter( "username",name );
        params.addParameter( "password",pwd );
        x.http().get( params,new Callback.CommonCallback <String>() {
            @Override
            public void onSuccess(String result) {
                showToast( "账号："+name+"  密码:"+pwd+"   登录成功" );
                username.setText( "" );
                password.setText( "" );
                yanzheng.setText( "" );
                ivShowCode.setImageBitmap(Code.getInstance().createBitmap());
                realCode = Code.getInstance().getCode().toLowerCase();
                Log.v(TAG,"realCode"+realCode);
            }

            @Override
            public void onError(Throwable ex,boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        } );
    }

}
